<?php


class ExampleController extends SmartL_Zend_Controller_Action 
{
	/**
	 * Action shows list of products.
	 */	 
    public function listAction() 
    {	
        $query = new Doctrine_Query(); // Create new query to database.   
		
        // This query is "filled" from action's configuration automatically loaded by Smart Framework
        $query->parseDqlQuery($this->config->getAction('list')
			->getSelection('list')->getDql('Db_Shop_Record_Product'));

		// View will get the paginator adapter, that is Zend Framework's compoment for pagination.
		// Smart Framework provides Doctrine adpater for this component.
        $products = new Zend_Paginator(new SmartL_Zend_Paginator_Adapter_Doctrine($query, Doctrine::HYDRATE_RECORD));
        $products->setCurrentPageNumber($this->_getParam('page'));
        $products->setItemCountPerPage(3);                             
        $this->view->products = $products;	// Pass it to the view
    }
        
    public function registerAction()
    {
		/* The Zend_Form is created from action's configuration automatically loaded by Smart Framework,
		SmartL_Zend_Form takes care of merging given translation of form's elements labels with validation errors translation which is alway the same. */
        $form = SmartL_Zend_Form::getForm($this->config->getAction("register")->getForm("registerForm"), $this->translations['default']);
        $this->view->form = $form;
		
		if ( $this->_request->isPost() ) {
			if ( $form->isValid($_POST) ) {
				/*If request is post and the form is valid, we want to insert new user into database,
				which means bind data from form to user's doctrine model. 
				Smart Framework helps here again with it's component SmartL_DataBinber. */			
        		$user = new Db_Record_User();
        		SmartL_DataBinder::update($form, 'zendForm', $user, 'doctrine',
        			array("username","password","fullName","email","address"));				
				/* ^ This example means: bind data from $form, which is handled by 'zendForm' DataBinder adapter,
				to $user, which is handled by 'doctrine' adapter. Last parameter is list of fields which should be binded. */				
				$user->save();
			}
		}
    }
}
